package com.dfrz.bookstore.service.impl;

import com.dfrz.bookstore.dao.impl.BookDaoImpl;
import com.dfrz.bookstore.dao.impl.ShoppingCarDaoImpl;
import com.dfrz.bookstore.entry.Book;
import com.dfrz.bookstore.entry.ShoppingCar;
import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.ShoppingCarService;

import javax.swing.*;
import java.util.List;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/21 15:45
 */
public class ShoppingCarServiceImpl implements ShoppingCarService {
    ShoppingCarDaoImpl shoppingCarDao = new ShoppingCarDaoImpl();
    BookDaoImpl bookDao = new BookDaoImpl();
    @Override
    public void insertShoppingCar(User user, Book book,int num) {
        /*计算图书价格*/
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setUserId(user.getId());
        shoppingCar.setBookId(book.getId());
        shoppingCar.setBookNum(num);
        shoppingCar.setBookPrice(book.getPrice());//购物车显示单价,订单项里面每本书计算合计
        shoppingCar.setBookName(book.getName());
        boolean flag = shoppingCarDao.insert(shoppingCar);
        if (flag){
            JOptionPane.showMessageDialog(null,"加入购物车成功");

        }else {
            JOptionPane.showMessageDialog(null,"加入购物车失败");
        }
    }

    @Override
    public void delectShoppingCarById(int id) {
        boolean flag = shoppingCarDao.deleteObjById(id);
        if (flag){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }

    }

    @Override
    public void upDataShoppingCar(ShoppingCar shoppingCar) {
        boolean flag = shoppingCarDao.updateByObj(shoppingCar);
        if (flag){
//            JOptionPane.showMessageDialog(null,"添加成功");
            System.out.println("添加成功");
        }else {
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

    @Override
    public List<ShoppingCar> findCarByCar(ShoppingCar shoppingCar) {
        List<ShoppingCar> shoppingCarList = shoppingCarDao.findObjByObj(shoppingCar);
        return shoppingCarList;
    }

    @Override
    public List<ShoppingCar> findCarByPage(ShoppingCar shoppingCar, int pageNum, int pageSize) {
        List<ShoppingCar> shoppingCarList = shoppingCarDao.findObjByPage(shoppingCar, pageNum, pageSize);

        return shoppingCarList;
    }
}
